package com.most.goods.controller;

import com.alibaba.fastjson.JSON;
import com.most.api.UserClient;
import com.most.goods.dto.Goods;
import com.most.goods.service.GoodsService;
import com.most.redis.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import nonapi.io.github.classgraph.json.JSONUtils;
import org.apache.tomcat.util.json.JSONParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/sysGoods")
@RefreshScope
@Api(tags = "商品模块")
public class GoodsController {
    @Value("${goods:#{null}}")
    private String goodsName;

    @Autowired
    RedisUtil redisUtil;

    @GetMapping("/getGoods")
    @ResponseBody
    public String getGoods() {
        return goodsName;
    }

    @Autowired
    UserClient userClient;
    @Autowired
    GoodsService goodsService;

    @GetMapping("/getUser")
    @ResponseBody
    public String getUser() {
        return userClient.getUser();
    }


    @GetMapping("/addGoods")
    @ResponseBody
    public Goods addGoods() {
        Goods goods = new Goods();
        goods.setName("商品1");
        boolean save = goodsService.save(goods);
        return goods;
    }

    @GetMapping("/list")
    @ResponseBody
    @ApiOperation("商品列表")
    public List<Goods> list() {
        return goodsService.list();
    }

    @GetMapping("/setRedis")
    @ResponseBody
    @ApiOperation("商品缓存")
    public void setRedis() {
        Goods goods = new Goods();
        goods.setName("goods1");
        goods.setId(1L);
        boolean boo = redisUtil.set("goods1", goods);
        if (boo) {
            Object goods11 = redisUtil.get("goods1");
            System.out.println(goods11);
        }

    }
}